﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - Subject" 
*/ 

component 
	displayname="public.service.db.senate.sequence.Subject" 
	output=false 
	accessors=true 
	{

	property type="string" name="studentLevel" hint="培养级别";
	property type="string" name="termStart" hint="招生学期";
	property type="string" name="subjectID" hint="生成专业方向号的专业ID";

	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "根据一个专业ID生成个专业方向ID"
	*/
	public string function getDirID() output=false {

		var nextID = getSubjectID() & "1";

		var sql = "	SELECT
						sbj.sbj_id
					FROM
						t_subject sbj
					WHERE
						sbj.sbj_id LIKE :condition 
					ORDER BY
						sbj.sbj_id DESC";
		
		var queryObj = new Query( datasource=application.dnsMaster, maxRows = 1);
		queryObj.addParam( name="condition", value=getSubjectID() & "_", cfsqltype="cf_sql_varchar" );
        var rs_check = queryObj.execute( sql=sql ).getResult();
		
		/* 
			专业方向生成规则 
			
			专业方向代码为6位, 前五位专业代码 余下1位为流水号 换言之 每一个专业至多设置9个专业主修方向
			
		*/
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = getSubjectID() & (right(rs_check.sbj_id, 1) + 1);
		}

		return nextID;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		var nextID = getStudentLevel() & getTermStart() & numberFormat(1, "0__");
		
		var sql = "	SELECT
						sbj.sbj_id
					FROM
						t_subject sbj
					WHERE
						sbj.sbj_id LIKE :condition 
					ORDER BY
						sbj.sbj_id DESC";
		
		var queryObj = new Query( datasource=application.dnsMaster, maxRows = 1);
		queryObj.addParam( name="condition", value=getStudentLevel() & getTermStart() & "___", cfsqltype="cf_sql_varchar" );
        var rs_check = queryObj.execute( sql=sql ).getResult();
		
		/* 
			主键生成规则 
			
			专业代码为5位, 第一位 本专科 第二位 招生学期 余下3位为流水号 换言之 每一个学校至多设置999个招生专业
			
		*/
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = getStudentLevel() & getTermStart() & numberFormat((right(rs_check.sbj_id, 3) + 1), "0__");
		}
		return nextID;
	}

}

</cfscript>